__author__ = 'user'
from selenium.webdriver.common.by import By

from pages import BasePage


class YandexMainPageLocators(object):
    searchTextField = (By.ID, r'text')
    searchButton = (By.XPATH, r'//*[@tabindex="2"]')
    resultReference = (By.XPATH, r'//*[@class="link serp-item__title-link"]')


class YandexMainPage(BasePage):

    def __init__(self, driver):
        super(YandexMainPage, self).__init__(driver)
        self.PAGE_ADDRESS = r'http://www.yandex.by/'

    def enterStringToSearchTextField(self, stringToEnter):
        searchTextField = self.searchElementsByLocator(YandexMainPageLocators.searchTextField)

        if searchTextField is not None:
            searchTextField.send_keys(stringToEnter)

    def clickSearchButton(self):
        searchButton = self.searchElementsByLocator(YandexMainPageLocators.searchButton)
        if searchButton is not None:
            searchButton.click()

    def getAmountOfResultReferences(self):
        resultReferences = self.searchElementsByLocator(YandexMainPageLocators.resultReference)

        if resultReferences is not None:
            amountOfResultReferences = len(resultReferences)
        else:
            amountOfResultReferences = 0

        return amountOfResultReferences